' options.bmx

Strict

Import "host.bmx"
Import "bytebuffer.bmx"
Import "nodeview.bmx"
Import "navbar.bmx"
Import "style.bmx"

Type TOption Extends TGadgetView

' TStyle interface

	Method Poll()
	End Method

	Method ToString$()
		Local	buffer:TByteBuffer
	End Method
	
	Method FromString(s$)
	End Method

End Type

Type TOptionsPage Extends TOption
	Field	owner:TOptionsRequester
	Field	dirty
	
	Method New()
		visible=True
	End Method
	
	Method AddPage(page:TOptionsPage)
		page.owner=owner
		page.gadget=owner.CreatePanel()
		page.SetPosition owner.panelx,owner.panely
		page.SetSize owner.panelw,owner.panelh
		page.SetVerticalFlow
		HideGadget page.gadget
		Append page
		Refresh
		owner.rootpage.Refresh
	End Method
	
	Method ShowPage()
		Layout
		Reshape
		ShowGadget gadget
	End Method
	
	Method HidePage()
		HideGadget gadget
	End Method	
	
	Method Invoke(command$,argument:Object=Null)
		Select command
			Case "ACTIVATE","SELECT"
				owner.OpenPage Self
		End Select
	End Method
End Type

Type TOptionsRequester Extends TRequester

	Field	rootpage:TOptionsPage
'	Field	rootnode:TNode
	Field	nodeview:TNodeView
	Field	currentpage:TOptionsPage
	Field	panelx,panely,panelw,panelh
	Field	treeview:TGadget
'	Field	pageview:TScrollView
	Field	dirty

	Method CreateOptionsRequester:TOptionsRequester(host:THost,w=640,h=380)
		InitRequester host,"Options",640,400
		w=ClientWidth(window)
		h=ClientHeight(window)-50
'		rootnode=TNode.Create("Options")
		rootpage=New TOptionsPage
		rootpage.owner=Self
		rootpage.SetName("Options")
		nodeview=TNodeView.CreateNodeView(window,rootpage)
		treeview=nodeview.treeview
		SetGadgetShape treeview,4,4,210,h
'		SetGadgetShape treeview,0,0,210,h
		rootpage.setnode TreeViewRoot(treeview),0
		panelx=230'38
		panely=6
		panelw=w-246-10
		panelh=h
'		pageview=TPageView.CreatePageView(window)
		Return Self
	End Method

	Method OpenPage(page:TOptionsPage)
		If currentpage And currentpage.gadget
			currentpage.HidePage
'		 HideGadget currentpage.gadget
		EndIf
		If page 
			If Not page.parent rootpage.AddPage page	'rootnode.Append page			
			page.ShowPage
		EndIf
		currentpage=page
		dirty=True
	End Method
		
	Method ClosePage(page:TOptionsPage)
	End Method

	Method CreatePanel:TGadget()
		Return maxgui.maxgui.CreatePanel(panelx,panely,panelw,panelh,window)	
	End Method
	
	Method Read(stream:TStream)
		If stream.ReadLine()="[Options]"
			rootpage.Read stream
		EndIf
	End Method
	
	Method Write(stream:TStream)
		stream.WriteLine "[Options]"
		rootpage.Write stream,"~t"
		stream.WriteLine ""
	End Method

	Method OnEvent()
		Select EventSource()		
			Case treeview
				nodeview.OnEvent
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case ok
				If dirty host.RefreshAll
				Hide
			Case cancel
				Hide
			Default
				If currentpage 
					currentpage.Poll
				EndIf
		End Select
	End Method

End Type
